package cn.zhiwen.clientController;

import cn.zhiwen.entity.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

@RestController
public class ClientController {


    //日志的打印
    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private DiscoveryClient clients;

    @RequestMapping("index")
    public String indexs() throws InterruptedException {
        return "Hello World";
    }


    @RequestMapping("hello")
    public String index() throws InterruptedException {
//        List<ServiceInstance> instances = clients.getInstances("");
//        for (ServiceInstance instance : instances) {
//            logger.info("/hello:host:"+instance.getHost()+"，service_id:"+instance.getServiceId());
//        }
        ServiceInstance localServiceInstance = clients.getLocalServiceInstance();
        //让线程等待几秒钟
        int sleepTime = new Random().nextInt(3000);
        logger.info("sleepTime:"+sleepTime);
        Thread.sleep(sleepTime);
        logger.info(localServiceInstance.getHost()+"========="+localServiceInstance.getServiceId());
        return "Hello World";
    }

    @RequestMapping("user")
    public String userDemo(User user) throws InterruptedException {
        ServiceInstance localServiceInstance = clients.getLocalServiceInstance();
        //让线程等待几秒钟
        //int sleepTimes = new Random().nextInt(4000);
        //logger.info("sleepTime:"+sleepTimes);
        //Thread.sleep(sleepTimes);
        logger.info(localServiceInstance.getHost()+"========="+localServiceInstance.getServiceId());
        return "id:"+user.getId().toString();
    }


}
